<?php
	require_once 'common.php';

	class File
	{    
		protected $FFilename;
		protected $FFileDoesntExist = "Requested file doesn't exist";
		
		/**
		 * Kthen pathin ne te cilin ndodhet skedari 
		 * 
		 * psh: root/libs
		 */
		function getPath()
		{
			$f = explode('/', $this->FFilename);
				  
			array_pop($f);
			
			return (string)implode('/', $f);     
		}
	
		/**
		 * Kthen prapashtesen e skedarit
		 */
		function getExt()
		{
			$ext = explode('.', $this->FFilename);

			return $ext[count($ext) - 1];
		}
		
		/**
		 * Kthen emrin e plote te skedarit
		 */ 
		function getFullFilename()
		{
			return $this->FFilename;
		}
		
		/**
		* Setter per $this->FFilename
		* 
		* @param string $newFilename
		*/
		function setFullFilename($newFilename)
		{
			$this->FFilename = $newFilename;
		}
		
		/**
		 * Kthen emrin e skedarit
		 * 
		 * @param mixed $showExt (optional; default = true)
		 * Nese eshte true atehere kthehet dhe
		 * parpashtesa, ndryshe anashkalohet
		 * 
		 * @return string
		 */
		function getFilename($showExt = true)
		{
			$f = explode('/', $this->FFilename);
			$f = $f[count($f) - 1];   
			
			if ($showExt)
				return $f;
			else
			{
				return substr($f, 0, lastDelimiter('.', $f)); 
			}
		}
		
		/**
		 * Kthen madhesine ne byte te skedarit
		 * krijon exception nese skedari nuk ekziston
		 */
		function getSize()
		{
			if ($this->exists())  
				return filesize($this->FFilename);
				
			throw new Exception($this->FFileDoesntExist);
		}
		
		/**
		 * Kthen true nese skedari ekziston
		 */
		function exists()
		{         
			return is_file($this->FFilename) && file_exists($this->FFilename);
		}
		
		/**
		 * Riemerton skedarin ne newname. kthen boolean
		 * bazuar ne suksesin e veprimit
		 * $newname duhet te jete VETEM emri i skedarit
		 */
		function ren($newname)
		{
			$newFn = $this->getPath() . '/' . $newname;
			
			$b = rename($this->FFilename, $newFn);
			
			if ($b)
				$this->FFilename = $newFn;
			
			return $b;
		}
		
		/**
		 * Ndryshon prapashtesen e skedarit
		 */
		function changeExt($newExt)
		{
			return $this->ren( $this->filename(false) . '.' . $newExt);
		}
		
		/**
		 * Ndryshon emrin e skedarit pa ndryshuar prapashtesen
		 * 
		 * psh: FFileName = 'hello.php'; 
		 *      changeName('hi');
		 *      FFileName -> hi.php
		 */  
		function changeName($newName)
		{
			return $this->ren( $newName . '.' . $this->ext());
		} 
		
		/**
		 * Fshin skedarin
		 */ 
		function delete()
		{
			return unlink($this->FFilename);
		}
		
		/**
		 * Kopjon skedarin
		 */
		function copyfile($copyto)
		{
			return copy($this->FFilename, $copyto);
		}
		
		/**
		 * Leviz skedarin
		 * newFn duhet te jete direktori + emri i skedarit
		 */
		function move($newFn)
		{
			$b = $this->copyfile($newFn);
			
			if ($b)
			{
				$b = $this->delete();
				
				if ($b)
					$this->FFilename = $newFn; 
					
				return $b;
			}
				
			return false;
		}
		
		/**
		 * Leviz skedarin ne direktorine newPath duke
		 * mbajtur emrin ekzistues
		 */
		function moveToDir($newPath)
		{
			return $this->move($newPath . '/' . $this->filename());
		}
		
		/**
		 * Kthen permajtjen e skedarit
		 * 
		 * Nese escapeHtmlChars eshte true atehere
		 * funksioni ben 'escape' entitetet e HTML
		 */
		function getContents($escapeHtmlChars = true)
		{
			if (!$this->exists())
				throw new Exception($this->FFileDoesntExist);

			$fc =  file_get_contents($this->FFilename);
			
			if ($escapeHtmlChars)
				return htmlspecialchars($fc);
			else
				return $fc;
		}
		
		/**
		 * Mbivendos permbajtjen e skedarit
		 */
		function setContents($cont)
		{
			return @file_put_contents($this->FFilename, $cont) !== false;
		}
		
		protected function internalDownload($url)
		{
			$f = new File($url);      
			
			header ("Content-Type: " . getMime($f->getExt()));
			header ("Accept-Ranges: bytes");
			header ("Content-Length: ". $f->getSize());
			header ("Content-Disposition: attachment; filename=\"". $f->getFilename() . '"');

			ob_clean();
			ob_start();
			readfile($url); 
			
			ob_flush();  

		}
		
		/**
		 * Shkarkon skedarin
		 */
		function download()
		{                
			$this->internalDownload($this->getFullFilename());
		}
		
		/**
		* Shkarkon skedarin si arkiv Zip
		* skedari ngarkohet ne OutputBuffer dhe pastaj fshihet
		*/
		function downloadAsZip()
		{
			// Krijojme nje arkiv te ri .zip
			// me emer emrin e skedarit dhe prapashtese.zip   
			$za = new ZipArchive();
			
			// Kalojme argumentin false qe te mos e marrim prapashtesen e skedarit
			$zipFn = $this->getFilename(false) . '.zip';
			
			// Hapim arkivin
			if ($za->open($zipFn, ZipArchive::CREATE) === true)
			{               
				// Shtojme skedarin.
				// E shtojme me emer ne menyre qe ne zip te mos krijohen direktori                                 
				$za->addFile($this->getFullFilename(), $this->getFilename());
				
				// Mbyllim arkivin
				$za->close();
				
				// Therrasim metoden e shkarkimit
				$this->internalDownload($zipFn);
				
				// Fshijme zip-in e prodhuar
				// nese skedari nuk do fshihet atehere ai do ngelet ne server
				//
				// Skedari mund te fshihet edhe pse ai mund te jete ende duke u shkarkuar
				// Skedari eshte derguar te klienti me ane te output buffer nga internalDownload
				unlink($zipFn);
			}
			
		}
		
		/**
		 * Shton stringun s te skedari
		 */
		function append($s)
		{
			$f = fopen($this->FFilename, "a");
			fwrite($f, $s);
			fclose($f);
		}
		
		/**
		 * Shton stringun line te skedari duke shtuar 
		 * nje rrjesht ne fund
		 */
		function appendLn($line)
		{
			$this->append($line . "\n");
		}
		
	  
		/**
		 * Konstruktori.
		 * 
		 * filename eshte skedari me te cilin do punojme
		 */
		function File($filename)
		{
			$this->FFilename = $filename;  
		}
	}
?>
